//
//  AdvertisingView.m
//  WanCaiC8
//
//  Created by tangang on 2018/5/7.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "Line_AdvertisingView.h"

@implementation Line_AdvertisingView{
    UIView  *bgView;
}

- (instancetype)init{
    if (self=[super init]) {
        
    }
    return self;
}

- (void)onclickPush:(UITapGestureRecognizer *)tap{
    AdvertisingModel *model=self.dataArr[tap.view.tag];
    
    if ([model.Title containsString:@"】"]) {
        
        UIPasteboard *copy = [UIPasteboard generalPasteboard];
        NSRange startRange = [model.Title rangeOfString:@"【"];
        NSRange endRange = [model.Title rangeOfString:@"】"];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        NSString *result = [model.Title substringWithRange:range];
        
        [copy setString:result];
        
        [SVProgressHUD showSuccessWithStatus:@"复制成功"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
            
        });
    }else{
        if (model.TransferUrl.length<1) {
            return;
        }
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];
    }
    
    //    PublicWebVC *vc=[[PublicWebVC alloc]init];
    //    vc.url=model.TransferUrl;
    //    [[TGTools viewController:self].navigationController pushViewController:vc animated:YES];
}

- (void)setDataArr:(NSMutableArray *)dataArr{
    _dataArr=dataArr;
    if (dataArr.count==0) {
        return;
    }
    [bgView removeFromSuperview];
    bgView=[[UIView alloc]init];
    bgView.backgroundColor=[UIColor whiteColor];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    
    float   height=40;
    for (int i=0; i<dataArr.count; i++) {
        
        UIView *view=[self viewWithModel:dataArr[i]];
        view.tag=i;
        
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickPush:)];
        [view addGestureRecognizer:tap];
        
        [bgView addSubview:view];
        //        float viewHeight=[self getViewHeight:dataArr[i]];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(bgView);
            make.top.equalTo(bgView).with.offset(height*i);
            make.height.mas_equalTo(@(height));
        }];
        
//        UIView *line2=[[UIView alloc]init];
//        line2.backgroundColor=DEFAULTBGCOLOR;
//        [bgView addSubview:line2];
//        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.bottom.equalTo(bgView);
//            make.height.mas_equalTo(@.7);
//        }];
        UIView *leftLine=[[UIView alloc]init];
        leftLine.backgroundColor=DEFAULTBGCOLOR;
        [view addSubview:leftLine];
        [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(view);
            make.left.equalTo(view).offset(10);
            make.width.mas_equalTo(@.7);
        }];
        UIView *rightLine=[[UIView alloc]init];
        rightLine.backgroundColor=DEFAULTBGCOLOR;
        [view addSubview:rightLine];
        [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(view);
            make.right.equalTo(view).offset(-7);
            make.width.mas_equalTo(@.7);
        }];
        //        height+=viewHeight;
    }
    if (self.changeHeight) {
        self.changeHeight(real_size(40*self.dataArr.count));
    }
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(bgView);
        make.height.mas_equalTo(@.7);
    }];
    
    
}


- (UIView *)viewWithModel:(AdvertisingModel *)model{
    UIView *view;
    switch (model.ThumbStyle) {
        case 0:
            view=[self noImage:model];
            break;
        case 1:
            view=[self oneSmallImage:model];
            break;
        case 2:
            view=[self oneBigImage:model];
            break;
        case 3:
            view=[self moreSmallImage:model];
            break;
        case 4:
            view=[self onlyImage:model];
            break;
        default:
            break;
    }
    return view;
}
//
//- (float)getViewHeight:(AdvertisingModel *)model{
//    float height=0;
//    if (model.ThumbStyle==0) {
//        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27 andLineSpacing:5]+55;
//        if (self.noTimel) {
//            height-=33;
//        }
//    }else if (model.ThumbStyle==1){
//        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27-125 andLineSpacing:5]+55;
//
//        if (self.noTimel) {
//            height-=33;
//        }
//
//        if (height<95) {
//            height=95;
//        }
//
//    }else if (model.ThumbStyle==2){
//        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27 andLineSpacing:5]+50+(SCREEN_WIDTH-22)/394.0*160;
//        if (self.noTimel) {
//            height-=33;
//        }
//    }else if (model.ThumbStyle==3){
//        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27 andLineSpacing:5]+50+(SCREEN_WIDTH-80)/3.0/105*75;
//        if (self.noTimel) {
//            height-=33;
//        }
//    }else if (model.ThumbStyle==4){
//        height=(SCREEN_WIDTH-22)/394.0*160+10;
//    }
//
//    return height;
//}

///无图
- (UIView *)noImage:(AdvertisingModel *)model{
    UIView *advertView=[[UIView alloc]init];
    QXLable *labTitle=[[QXLable alloc]initGaryText];
    labTitle.numberOfLines=0;
    
    if ([model.Title containsString:@"【"]) {
        labTitle.attributedText = [NSAttributedString tg_imageTextWithTitle:model.Title];
    } else {
        labTitle.text=model.Title;
    }
    
    //    [labTitle setSpace:5];
    [advertView addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(advertView);
        make.left.equalTo(advertView).with.offset(17);
        make.right.equalTo(advertView).with.offset(-17);
    }];
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [advertView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(advertView);
        make.height.mas_equalTo(@.7);
    }];
    
    
    return  advertView;
}



///一张小图
- (UIView *)oneSmallImage:(AdvertisingModel *)model{
    UIView *advertView=[[UIView alloc]init];
    UIImageView *imageV=[[UIImageView alloc]init];
    if (model.ThumbList.count>0) {
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.ThumbList[0]]];
    }
    [advertView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(advertView).with.offset(-20);
        make.centerY.equalTo(advertView);
        make.size.mas_equalTo(CGSizeMake(105, 75));
    }];
    
    QXLable *labTitle=[[QXLable alloc]initTitle];
    labTitle.numberOfLines=0;
    labTitle.text=model.Title;
    [labTitle setSpace:5];
    [advertView addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(advertView).with.offset(11);
        make.left.equalTo(advertView).with.offset(10);
        make.right.equalTo(imageV.mas_left).with.offset(-17);
    }];
    
    if (!self.noTimel) {
        QXLable *labTime=[[QXLable alloc]initWithFount:13];
        labTime.text=[NSString stringWithFormat:@"%@  %@  %ld  ",model.Company,model.SubTime,model.CommentsNumber];
        [advertView addSubview:labTime];
        [labTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTitle);
            make.top.equalTo(labTitle.mas_bottom).with.offset(20);
        }];
        
        UIImageView *imageComments=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"comments"]];
        [advertView addSubview:imageComments];
        [imageComments mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTime.mas_right);
            make.centerY.equalTo(labTime);
            make.size.mas_equalTo(imageV.size);
        }];
    }
    
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [advertView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(advertView);
        make.height.mas_equalTo(@.7);
    }];
    
    return advertView;
}

///一张大图
- (UIView *)oneBigImage:(AdvertisingModel *)model{
    UIView *advertView=[[UIView alloc]init];
    QXLable *labTitle=[[QXLable alloc]initTitle];
    labTitle.numberOfLines=0;
    labTitle.text=model.Title;
    [labTitle setSpace:5];
    [advertView addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(advertView).with.offset(11);
        make.left.equalTo(advertView).with.offset(10);
        make.right.equalTo(advertView).with.offset(-17);
    }];
    
    UIImageView *imageV=[[UIImageView alloc]init];
    if (model.ThumbList.count>0) {
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.ThumbList[0]]];
    }
    [advertView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(advertView);
        make.top.equalTo(labTitle.mas_bottom).with.offset(10);
        make.height.mas_equalTo(@((SCREEN_WIDTH-22)/394.0*160));
    }];
    
    if (!self.noTimel) {
        QXLable *labTime=[[QXLable alloc]initWithFount:13];
        labTime.text=[NSString stringWithFormat:@"%@  %@  %ld  ",model.Company,model.SubTime,model.CommentsNumber];
        [advertView addSubview:labTime];
        [labTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTitle);
            make.top.equalTo(imageV.mas_bottom).with.offset(20);
        }];
        
        UIImageView *imageComments=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"comments"]];
        [advertView addSubview:imageComments];
        [imageComments mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTime.mas_right);
            make.centerY.equalTo(labTime);
            make.size.mas_equalTo(imageComments.size);
        }];
    }
    
    
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [advertView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(advertView);
        make.height.mas_equalTo(@.7);
    }];
    return advertView;
}

///多张小图
- (UIView *)moreSmallImage:(AdvertisingModel *)model{
    UIView *advertView =[[UIView alloc]init];
    QXLable *labTitle=[[QXLable alloc]initTitle];
    labTitle.numberOfLines=0;
    labTitle.text=model.Title;
    [labTitle setSpace:5];
    [advertView addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(advertView).with.offset(11);
        make.left.equalTo(advertView).with.offset(10);
        make.right.equalTo(advertView).with.offset(-17);
    }];
    
    for (int i=0; i<3; i++) {
        UIImageView *imageV=[[UIImageView alloc]init];
        if (model.ThumbList.count>i) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:model.ThumbList[i]]];
        }else{
            break;
        }
        [advertView addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(advertView).with.offset((SCREEN_WIDTH-80)/3.0/105*75*i+10*i+10);
            make.top.equalTo(labTitle.mas_bottom).with.offset(10);
            make.height.mas_equalTo(@((SCREEN_WIDTH-80)/3.0/105*75));
        }];
    }
    
    if (!self.noTimel) {
        QXLable *labTime=[[QXLable alloc]initWithFount:13];
        labTime.text=[NSString stringWithFormat:@"%@  %@  %ld  ",model.Company,model.SubTime,model.CommentsNumber];
        [advertView addSubview:labTime];
        [labTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTitle);
            make.top.equalTo(labTitle.mas_bottom).with.offset(20+(SCREEN_WIDTH-80)/3.0/105*75);
        }];
        
        UIImageView *imageComments=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"comments"]];
        [advertView addSubview:imageComments];
        [imageComments mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTime.mas_right);
            make.centerY.equalTo(labTime);
            make.size.mas_equalTo(imageComments.size);
        }];
    }
    
    
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [advertView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(advertView);
        make.height.mas_equalTo(@.7);
    }];
    
    
    return advertView;
}

- (UIView *)onlyImage:(AdvertisingModel *)model{
    UIView *advertView=[[UIView alloc]init];
    UIImageView *imageV=[[UIImageView alloc]init];
    if (model.ThumbList.count>0) {
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.ThumbList[0]]];
    }
    [advertView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(advertView);
        make.top.equalTo(advertView).with.offset(10);
        make.height.mas_equalTo(@((SCREEN_WIDTH-22)/394.0*160));
    }];
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [advertView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(advertView);
        make.height.mas_equalTo(@.7);
    }];
    
    return advertView;
    
}


@end

